import Foundation
import UIKit

// 测试协议
protocol UserManagerDelegate: AnyObject {
    func userDidLogin(_ user: User)
    func userDidLogout()
}

// 测试类
public class UserManager: NSObject {
    static let shared = UserManager()
    
    weak var delegate: UserManagerDelegate?
    private var currentUser: User?
    
    // 测试属性
    @objc dynamic var isLoggedIn: Bool = false
    var loginAttempts: Int = 0
    
    // 测试方法
    func login(username: String, password: String) -> Bool {
        loginAttempts += 1
        
        if validateCredentials(username: username, password: password) {
            currentUser = User(name: username)
            isLoggedIn = true
            delegate?.userDidLogin(currentUser!)
            return true
        }
        return false
    }
    
    @objc func logout() {
        currentUser = nil
        isLoggedIn = false
        delegate?.userDidLogout()
    }
    
    private func validateCredentials(username: String, password: String) -> Bool {
        // 测试私有方法
        return !username.isEmpty && password.count >= 6
    }
    
    // 测试泛型
    func updateUserInfo<T: Codable>(_ info: T) {
        print("Updating user info: \(info)")
    }
}

// 测试结构体
struct User: Codable {
    let name: String
    var email: String?
    var age: Int = 0
    
    // 测试计算属性
    var displayName: String {
        return "@\(name)"
    }
}

// 测试枚举
enum LoginError: Error {
    case invalidUsername
    case invalidPassword
    case networkError(String)
}

// 测试扩展
extension UserManager {
    func resetPassword(for email: String) {
        print("Reset password for: \(email)")
    }
}
